package com.jevons.geo_edit.util;

import android.os.Environment;

import com.jevons.muffin.BaseApplication;

import java.io.File;

/**
 * @file FileName
 * FFireApp
 * Created by Tangkhun on 2018/8/17.
 * <p>Copyright 2018 JxGIS.</p>
 * Description：
 */
public class FilePathUtils {

    /**
     * 文件保存的 根路径
     */
    public static final String FILE_ROOT;

    /**
     * 日志文件的路径
     */
    public static final String LOG_ROOT;

    /**
     * 下载文件保存的路径
     */
    public static final String DOWN_PATH;

    /**
     * 缓存文件保存路径
     */
    public static final String CACHE_PATH;

    /**
     * 数据库文件夹路径
     */
    public static final String DATABASE_FOLDER_PATH;

    public static final String APP_CACHE_PATH;

    static {
        FILE_ROOT = Environment.getExternalStorageDirectory() + "/" + "geo_package_sdk" + "/";
        DOWN_PATH = FILE_ROOT + "attach/";
        LOG_ROOT = FILE_ROOT + "log";
        CACHE_PATH = FILE_ROOT + "cache/";
        DATABASE_FOLDER_PATH = BaseApplication.getContext().getExternalFilesDir("databases").getAbsolutePath();
        APP_CACHE_PATH = BaseApplication.getContext().getExternalCacheDir().getAbsolutePath();
    }

    public static String getCachePath() {
        return createDir(FILE_ROOT + "/Cache/").getPath();
    }

    public static String getTilePath() {
        return createDir(FILE_ROOT + "/Tiles/").getPath();
    }

    public static String getGeoJsonPath() {
        return createDir(FILE_ROOT + "/GeoJson/").getPath();
    }

    public static String getDownloadPath() {
        return createDir( FILE_ROOT + "/Download/").getPath();
    }


    public static String getShapeFilePath() {
        return createDir( FILE_ROOT + "/Shape/").getPath();
    }

    public static String getDatabasePath() {
        return createDir(DATABASE_FOLDER_PATH).getPath();
    }

    public static String getAppCachePath() {
        return createDir(APP_CACHE_PATH).getPath();
    }

    public static String getAppTempDatabasePath() {
        return createDir(APP_CACHE_PATH + "/.database").getPath();
    }


    public static String getProcessDatabase() {
        return createDir( FILE_ROOT + "/ProcessDatabase/").getPath();
    }


    /**
     * 创建文件夹
     *
     * @return File
     */
    public static File createDir(String path) {
        File dir = new File(path);
        if (!isExist(dir)) {
            dir.mkdirs();
        }
        return dir;
    }


    /**
     * 判断File对象所指的目录或文件是否存在
     *
     * @return boolean
     */
    public static boolean isExist(File file) {
        return file.exists();
    }

    /**
     * 删除文件
     *
     * @param filePath
     * @return
     */
    public static boolean deleteFile(String filePath) {
        File file = new File(filePath);
        if (file.exists()) {
            file.delete();
            return true;
        }
        return false;
    }


}
